package com.tyust.web;


import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tyust.entity.PageUtils;
import com.tyust.entity.ResponseDto;
import com.tyust.entity.Supplier;
import com.tyust.service.SupplierService;
import com.tyust.util.commons.CommonUtil;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
	@Autowired
	private SupplierService supplierService;
	
	@RequestMapping("/add.do")
	@ResponseBody
	public ResponseDto add(@RequestBody Supplier supplier) throws SQLException{
		supplier.setSupplierNo(CommonUtil.uuid());
		supplierService.add(supplier);
		return ResponseDto.ok();
	}
	@RequestMapping("/searchSupplierInfo.do")
	@ResponseBody
	public ResponseDto searchSupplierInfo(Supplier supplier){
		PageUtils page = supplierService.searchSupplierInfo(supplier);
		return ResponseDto.ok().put("page", page);
	}
	
	@RequestMapping("/updateSupplierInfo.do")
	@ResponseBody
	public ResponseDto updateSupplierInfo(@RequestBody Supplier supplier){
		supplierService.updateSupplierInfo(supplier);
		return ResponseDto.ok();
	}
	
	@RequestMapping("/deleteByNo.do")
	@ResponseBody
	public ResponseDto deleteByNo(@RequestBody String[] supplierNos){
		for(String supplierNo:supplierNos) {
			supplierService.deleteByNo(supplierNo);
		}
		return ResponseDto.ok();
	}
	
	@RequestMapping("/getSupplierByNo.do")
	@ResponseBody
	public ResponseDto getSupplierByNo(String supplierNo){
		Supplier supplier=new Supplier();
		supplier.setSupplierNo(supplierNo);
		Supplier result = supplierService.getSupplierByNo(supplier);
		return ResponseDto.ok().put("result", result);
	}
}
